package com.kepler.social.web.controller;

import com.github.pagehelper.PageInfo;
import com.kepler.social.dao.domain.UserBaseInfo;
import com.kepler.social.service.manager.UserManager;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.testng.collections.Lists;

import java.util.List;

@RestController
@Slf4j
public class HomeTestController {

    @Resource
    private UserManager userManager;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "hellow welcome to kepler!";
    }

    @RequestMapping(value = "/getCtr", method = RequestMethod.GET)
    public Object getCtr(Long id) {
        UserBaseInfo userBaseInfo = userManager.getById(id);
        return userBaseInfo;
    }

    @RequestMapping(value = "/getPageCtr")
    public PageInfo<UserBaseInfo> getPageCtr(@RequestParam(value = "pageNum", defaultValue="1") int pageNum ){
        //pageNum:表示第几页  pageSize:表示一页展示的数据
        List<UserBaseInfo> list= userManager.selectAllByPage(pageNum, 6);
        //将查询到的数据封装到PageInfo对象
        PageInfo<UserBaseInfo> pageInfo = new PageInfo(list);
        //分割数据成功
        return pageInfo;
    }
//
//    @RequestMapping(value = "/getPageCr")
//    public List getPageCr(@RequestParam(value = "pageNum", defaultValue="1") int pageNum ){
//        //pageNum:表示第几页  pageSize:表示一页展示的数据
//        PageHelper.startPage(pageNum,3);
//        CxRgroupExample example = new CxRgroupExample();
//        example.setDistinct(false);
//        // 一个就够了 多了算or
//        example.getOredCriteria()
//                .add(new CxRgroupExample.Criteria(){{
//                    andIdBetween(0L, 110L);
//                }});
//        List<CxRgroup> list= cxTaskRecordService.selectByExample(example);
//        PageInfo<CxRgroup> pageInfo = new PageInfo(list,3);
//        return list;
//    }


    @RequestMapping(value = "/genData")
    public List<Integer> genData(){
        List<UserBaseInfo> list = Lists.newArrayList();
        for(int i = 20; i < 30 ; i++){
            list.add(UserBaseInfo.builder().nickName("你好" + i).build());
        }
        List<Integer> result =  userManager.insertMutile(list);
        Assert.notNull(list.get(0).getId(),"无获取主键");
        return result;
    }


//
//    /**
//     * 测试不启事务
//     * @return
//     */
//    @RequestMapping(value = "/updateNoTrans")
//    public String updateNoTrans(){
//        CxRgroup cxRgroup = new CxRgroup();
//        cxRgroup.setId(1L);
//        cxRgroup.setDesc("非事务修改1");
//        return iCxTaskGroupService.updateByPrimaryKeySelective(cxRgroup)  + "";
//    }
//
//    /**
//     * 测试启动事务失败
//     * @return
//     */
//    @RequestMapping(value = "/updateTrans")
//    public String updateTrans(){
//        CxRgroup cxRgroup = new CxRgroup();
//        cxRgroup.setId(1L);
//        cxRgroup.setDesc("事务修改失败");
//        try{
//            try (HintManager hintManager = HintManager.getInstance()){
//                hintManager.setMasterRouteOnly();
//                return iCxTaskGroupService.updateByPrimaryKeySelectiveWithTrans(cxRgroup)  + "";
//            }
//        }catch (Exception e){
//            return  e.getMessage();
//        }
//    }
//
    /**
     * 强制走主库
     * @return
     */
    @RequestMapping(value = "/foiceMaster")
    public UserBaseInfo foiceMaster(Long id){
        try (HintManager hintManager = HintManager.getInstance()){
            hintManager.setWriteRouteOnly();
            UserBaseInfo userBaseInfo = userManager.getById(id);
            return userBaseInfo;
        }
    }

    /**
     * 强制走主库
     * @return
     */
    @RequestMapping(value = "/update")
    public UserBaseInfo update(Long id, String name) {
        int i =  userManager.update(id, name);
        UserBaseInfo userBaseInfo = userManager.getById(id);
        return userBaseInfo;
    }

    @RequestMapping(value = "/updateAndGet")
    public UserBaseInfo updateAndGet(Long id, String name) {
        return  userManager.updateAndGet(id, name);
    }


    @RequestMapping(value = "/updateAndGetTrans")
    public UserBaseInfo updateAndGetTrans(Long id, String name) {
        return  userManager.updateAndGetTrans(id, name);
    }
}
